import { getToken, setToken, removeToken, removeMation, setMation } from '@/utils/auth'
import { login, getPhoneCode, register, resetPassword, getInfo, setInfo } from '@/api/user'

const state = {
    token: getToken(),
    user: {},
    name: null,
    loading: false,
    imgUrl: 'http://www.jssggxh.com:8082'
}
const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_USER: (state, user) => {
        state.user = user
    },
    SET_LOADING: (state, loading) => {
        state.loading = loading
    },
}
const actions = {
    //修改loading
    setLoading({ commit }, data) {
        commit('SET_LOADING', data)
    },
    login({ commit }, userInfo) {
        const { username, password } = userInfo
        return new Promise((resolve, reject) => {
            login({ mobile: username.trim(), password: password }).then(response => {
                const { data } = response
                commit('SET_TOKEN', data)
                setToken(data)
                setMation(JSON.stringify(userInfo))
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    getInfo({ commit }) {
        return new Promise((resolve, reject) => {
            getInfo().then(response => {
                const { data } = response
                commit('SET_NAME', data.mobile)
                commit('SET_USER', data)
                if (!data) {
                    reject('Verification failed, please Login again.')
                }
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    getPhoneCode({ commit }, data) {
        return new Promise((resolve, reject) => {
            getPhoneCode(data).then(response => {
                const { data } = response
                if (!data) {
                    reject('Verification failed, please Login again.')
                }
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    register({ commit }, data) {
        return new Promise((resolve, reject) => {
            register(data).then(response => {
                const { data } = response
                if (!data) {
                    reject('Verification failed, please Login again.')
                }
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    resetPassword({ commit }, data) {
        return new Promise((resolve, reject) => {
            resetPassword(data).then(response => {
                const { data } = response
                if (!data) {
                    reject('Verification failed, please Login again.')
                }
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    //更新个人信息
    setInfo({ commit }, data) {
        return new Promise((resolve, reject) => {
            setInfo(data).then(response => {
                const { data } = response
                // if (!data) {
                //     reject('Verification failed, please Login again.')
                // }
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },



    // remove token
    resetToken({ commit }) {
        return new Promise(resolve => {
            commit('SET_TOKEN', '')
            commit('SET_NAME', null)
            commit('SET_USER', {})
            removeToken()
            removeMation()
            resolve()
        })
    },
}


export default {
    namespaced: true,
    state,
    mutations,
    actions
}
